package vue.eleve;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import metier.Stage;
import ctr.commun.CommunGest;
import ctr.eleve.GestAjoutEleve;

public class JPanAjoutEleve extends JPanel{

	private static final long serialVersionUID = -8258905260255616984L;
	private JTextField saisiNomEleve, saisiPrenomEleve;
	private JComboBox saisiGradeEleve;
	private JComboBox saisiStage;
	private JTextField saisiDateNaissanceEleve;
	
	public JPanAjoutEleve(String titre) {
		CommunGest communGest = new CommunGest();
		ArrayList<Stage> lStage = communGest.getListeStage();
		// On test si des stages existe, sinon message erreur.
		if ( !lStage.isEmpty()){
			// Panel de saisi
			JPanel panelSaisi = new JPanel();
			panelSaisi.setLayout(new GridLayout(5,1,5,5));
			
			saisiNomEleve = new JTextField(15);
			saisiPrenomEleve = new JTextField(15);
			saisiGradeEleve = new JComboBox(communGest.getListeGrade().toArray());
			saisiDateNaissanceEleve = new JTextField(10);
			saisiStage = new JComboBox(lStage.toArray());
			
			panelSaisi.add(saisiNomEleve);
			panelSaisi.add(saisiPrenomEleve);
			panelSaisi.add(saisiGradeEleve);
			panelSaisi.add(saisiDateNaissanceEleve);
			panelSaisi.add(saisiStage);
			
			// Panel de label
			JPanel panelLabel = new JPanel();
			panelLabel.setLayout(new GridLayout(5,1,5,5));
			panelLabel.add(new JLabel("Nom : "));
			panelLabel.add(new JLabel("Prenom : "));
			panelLabel.add(new JLabel("Grade : "));
			panelLabel.add(new JLabel("Date de Naissance : "));
			panelLabel.add(new JLabel("Stage : "));
			
			JButton jButtonAjoutEleve = new JButton("Ajouter");
			jButtonAjoutEleve.addActionListener(new GestAjoutEleve(this));
			
			// Panel de label
			JPanel panelBouton = new JPanel();
			panelBouton.add(jButtonAjoutEleve);
			
			// Panel global
			// Avec mise en forme.
			this.setBorder(new TitledBorder(titre));
			this.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.weightx = 0;
			gbc.weighty = 0; 
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.fill = GridBagConstraints.VERTICAL;
			this.add(panelLabel,gbc);
			
			gbc.gridx = 1;
			gbc.gridy = 0;
			gbc.fill = GridBagConstraints.VERTICAL;
			this.add(panelSaisi,gbc);
			
			gbc.gridx = 0;
			gbc.gridy = 2;
			gbc.gridwidth = 2;
			gbc.gridheight = 2;
			gbc.fill = GridBagConstraints.CENTER ;
			this.add(panelBouton, gbc);
		}else{
			JOptionPane.showMessageDialog(this, "Aucun stage saisi, ajout d'eleve impossible", "Probleme", JOptionPane.WARNING_MESSAGE);
		}
	}

	public JTextField getSaisiNomEleve() {
		return saisiNomEleve;
	}

	public JTextField getSaisiPrenomEleve() {
		return saisiPrenomEleve;
	}

	public JComboBox getSaisiGradeEleve() {
		return saisiGradeEleve;
	}

	public JTextField getSaisiDateNaissanceEleve() {
		return saisiDateNaissanceEleve;
	}

	public JComboBox getSaisiStage() {
		return saisiStage;
	}
}
